/*
 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * This file is part of Harmattan SmashMiner OpenGL game application.
 * 
 * Harmattan SmashMiner OpenGL Example Application version 1.0.0
 *  
 * Latest update: 15.4.2011
 *
 * The Harmattan SmashMiner OpenGL example application demonstrates how to use 
 * the OpenGL ES in Harmattan devices.
 *
 * This example is provided as a starting point for 3rd party
 * developers (you) to ease the implementation of OpenGL based 
 * games. Ideas, parts of code or methodologies that this
 * example application uses can be freely used without restrictions.
 *
 * See file README.txt how to build and compile this example application
 * in the Harmattan SDK environment. 
 * 
 * See file INSTALL.txt to find out what is required to run this
 * application and how to install the application to the device or
 * alternatively in QEMU-emulator.
 *
 * The LICENSE.txt file explains the license this software is provided
 * with. A copy of the same license is included in this source file.
 * 
 */

/*
 * Copyright (C) 2011 by Nokia Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
\file Controls.qml
\brief This is the controls help view of the SmashMiner game.

See the \c controlsView at the main.qml view.

\sa main.qml
*/

import Qt 4.7

Item {
    id: controlsView

    // Signal to return main view
    signal back()

    // Vertical margins of the view area
    property int marginsVertical: height/20
    // Height of the view header
    property int headerHeight: marginsVertical*2
    // Horizontal margins of the view area
    property int marginsHorizontal: headerHeight+marginsVertical
    // Height of the buttons used at this view
    property int buttonHeight: headerHeight*0.9

    // Is the right arrow button currently visible
    property bool rightArrowButtonVisible: true
    // Is the left arrow button currently visible
    property bool leftArrowButtonVisible: false

    width: parent.width
    height: parent.height
    z: -10
    visible: true

    Image {
        id: background
        anchors.fill: parent
        source: "qrc:/images/1024x512_01.jpg"
    }

    ViewHeader {
        id: controlsHeader
        heading: qsTr("CONTROLS")
    }

    Rectangle {
        id: rectangleControlHelp

        width: parent.width
        anchors { left: parent.left; right: parent.right; leftMargin: marginsHorizontal; rightMargin: marginsHorizontal;
            top: controlsHeader.bottom; bottom: controlsFooter.top }
        color: "black"
        border { width: 5; color: "#333333" }

        Item {
            id: controlPages

            // Page of this view that is currently visible
            property int currentPage: 1

            // 1) Set number of help pages here
            // 2) Add a state for each page
            // 3) Add the new help page inside Item element after the last help page
            property int numberOfPages: 2

            // Move to next page of this view
            function nextPage() {
                if (currentPage < numberOfPages) {
                    soundEngine.playMenuSound();
                    currentPage++;
                }
            }

            // Move to previous page of this view
            function previousPage() {
                if (currentPage > 1) {
                    soundEngine.playMenuSound();
                    currentPage--;
                }
            }

            // Slot for currentPageChanged() signal emitted
            // from the change of the property currentPage
            onCurrentPageChanged: {
                controlPages.state = 'page' + currentPage;
                rightArrowButtonVisible = false;
                leftArrowButtonVisible = false;
                pageChangeTimer.start();
            }

            width: parent.width*numberOfPages
            height: parent.height
            anchors.top: parent.top;
            x: 0;

            // Animate property x change when changing the view pages
            transitions: [
                 Transition {
                      NumberAnimation { properties: "x"; duration: 500 }
                 }
            ]

            // States for pages -> different states change the x value and NumberAnimation is started.
            states: [
                State {
                    name: "page1"
                    PropertyChanges {
                        target: controlPages;
                        x: 0
                    }
                },

                State {
                    name: "page2"
                    PropertyChanges {
                        target: controlPages;
                        x: -(rectangleControlHelp.width+marginsHorizontal);
                    }
                }
            ]

            ControlsPage1 {
                id: controlsPage1
                width: rectangleControlHelp.width
                height: rectangleControlHelp.height
                anchors.top:  parent.top
                x: 0;
            }

            ControlsPage2 {
                id: controlsPage2
                width: rectangleControlHelp.width
                height: rectangleControlHelp.height
                color: "black"
                anchors.top:  parent.top
                x: rectangleControlHelp.width+marginsHorizontal;
            }

            // Page change timer for showing and hiding the ArrowButtons
            Timer {
                id: pageChangeTimer
                interval: 500
                onTriggered: {
                    if (controlPages.currentPage == 1) {
                        rightArrowButtonVisible = true;
                    } else if (controlPages.currentPage < controlPages.numberOfPages) {
                        leftArrowButtonVisible = true;
                        rightArrowButtonVisible = true;
                    } else if (controlPages.currentPage == controlPages.numberOfPages) {
                        leftArrowButtonVisible = true;
                    }
                }
            } // pageChangeTimer
        }

        // Sweep area for changing view pages by sweeping the screen
        MouseArea {
            id: sweepArea

            property int startX: -1
            property int minSweepLength: parent.width*0.1

            anchors.fill: parent
            focus: true
            onPressed: {
                // When the screen is touched store the start point
                startX = mouseX;
            }
            onReleased: {
                // When released calculate the lenght of the sweep
                var length = mouseX - startX;

                if (length > minSweepLength) {
                    // Sweeped left to right
                    controlPages.previousPage();
                } else if (length < -minSweepLength) {
                    // Sweeped right to left
                    controlPages.nextPage();
                }
                startX = -1;
            }
        } // sweepArea
    }

    ArrowButton {
        id: leftArrowButton
        width: buttonHeight*1.5
        height: buttonHeight*1.5
        visible:  leftArrowButtonVisible ? "true" : "false"
        rotation:  180
        anchors { right: rectangleControlHelp.left; verticalCenter: rectangleControlHelp.verticalCenter }
        onClicked: controlPages.previousPage()
    }

    ArrowButton {
        id: rightArrowButton
        width: buttonHeight*1.5
        height: buttonHeight*1.5
        visible: rightArrowButtonVisible ? "true" : "false"
        anchors { left: rectangleControlHelp.right; verticalCenter: rectangleControlHelp.verticalCenter }
        onClicked: controlPages.nextPage()
    }

    ViewFooter {
        id: controlsFooter

        width: parent.width
        height: headerHeight

        onBackClicked: {
            soundEngine.playMenuSound();
            back();
            if (controlPages.currentPage != 1) {
                controlPages.currentPage = 1;
            }
        }
    } // controlsFooter
}
